package com.centit.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class BaiduMap {
	
	public static String get(String urlAll,String charset){
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";//模拟浏览器
        try {
            URL url = new URL(urlAll);
            System.out.println(url);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.setReadTimeout(30000);
            connection.setConnectTimeout(30000);
            connection.setRequestProperty("User-agent",userAgent);
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(
                     is, charset));
             String strRead = null;
             while ((strRead = reader.readLine()) != null) {
                 sbf.append(strRead);
                 sbf.append("\r");
             }
             reader.close();
             result = sbf.toString();
            
     } catch (Exception e) {
         e.printStackTrace();
     }
        return result;
    }
  
  public static Map<String,String> jeixi(String msg){
	  Map<String,String> map=new HashMap();
      String json=msg;
      //JSONParser parser=new JSONParser();
      if(msg.toString().indexOf("xml")!=-1) {
    	  map.put("lng", "0");
          map.put("lat", "0");
      }else {
	      JSONObject obj=JSONObject.parseObject(msg);
//	      System.out.println(obj.toString());
	//      JSONObject temp = "{" sa ":[]}"
	      if(obj.toString().indexOf("[")!=-1) {
	    	  map.put("lng", "0");
	          map.put("lat", "0");
	      }else {
	    	  JSONObject result1=(JSONObject)obj.get("result");
	          if(result1==null) {
	        	  map.put("lng", "0");
	              map.put("lat", "0");
	          }else {
	        	  JSONObject location=(JSONObject) result1.get("location");    
	              map.put("status", obj.get("status").toString());
	              map.put("lng", location.get("lng").toString());
	              map.put("lat", location.get("lat").toString());
	          }
	      }
      }

      return map;
  }
 
 public static void main(String[] args){
     // TODO Auto-generated method stub
     //String url="http://api.map.baidu.com/place/v2/suggestion?query=湖北省武汉市洪山区广埠屯&region=湖北省&city_limit=true&output=json&ak=gRhqOOqPOQzvM8nMRnVoQswejvggglqY";
//     String url2="http://api.map.baidu.com/place/v2/suggestion?query=上海市浦东新区锦绣申江&region=上海市&city_limit=true&output=json&ak=gRhqOOqPOQzvM8nMRnVoQswejvggglqY";
//     String result=test.get(url2, "UTF-8");
//     List<Object> result1=test.jeixi(result);
//     for(int i=0;i<result1.size();i++){
//         System.out.println(result1.get(i));
//     }
 }
}
